import axios from "@/plugins/axios"
import CommentAdd from "@/model/CommentAdd"

export default {
  namespaced: true,
  state: {
    currentContent: null,
    talk: null,
    comment: null,
    childComment: null,
    inputContentFocus: false,
    currentTag: null,
    talkFilter: null
  },
  mutations: {
    addComment(state, content) {
      //使输入框失去焦点，隐藏
      const comment: CommentAdd = new CommentAdd(content, state.talk.id)
      if (state.comment) {
        comment.commentId = state.comment.id
        if (state.childComment) {
          comment.childCommentId = state.childComment.id
        }
      }
      axios.post('/comment/addComment', comment).then(res => {
        if (state.comment) {
          state.comment.childComments.unshift(res.data)
        } else {
          state.talk.comments.unshift(res.data)
        }
      })
    },

    setCurrentTag(state, currentTag) {
      state.currentTag = currentTag
    },
    setTalkFilter(state, talkFilter) {
      state.talkFilter = talkFilter
    },
    setTalk(state, talk) {
      state.talk = talk
      state.comment = null
      state.childComment = null
      state.currentContent = talk.content
      setTimeout(() => {
        state.inputContentFocus = true
      }, 0)
    },
    setComment(state, {talk, comment}) {
      state.talk = talk
      state.comment = comment
      state.childComment = null
      state.currentContent = comment.content
      setTimeout(() => {
        state.inputContentFocus = true
      }, 0)
    },
    setChildComment(state, {talk, comment, childComment}) {
      state.talk = talk
      state.comment = comment
      state.childComment = childComment
      state.currentContent = childComment.content
      setTimeout(() => {
        state.inputContentFocus = true
      }, 0)
    },
    inputContentBlur(state) {
      setTimeout(() => {
        state.inputContentFocus = false
      }, 0)
    }
  },
}
